﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using GameView.Comment;
using GameView.Services;
using GameView.Services.Dtos.Blacklist;
using GameView.Views;
using System.Collections.ObjectModel;

namespace GameView.ViewModels
{
    public partial class BlacklistViewModel : ObservableValidator
    {
        private readonly BlacklistService _service;

        public BlacklistViewModel(BlacklistService service)
        {
            _service = service;
            LoadFromApi().ConfigureAwait(false);
        }

        private List<BlacklistShowModel> allData = [];

        [ObservableProperty]
        private bool _isLoading = false;

        [ObservableProperty]
        private ObservableCollection<BlacklistShowModel> _blacklists = [];

        [ObservableProperty]
        private string _searchStr = string.Empty;

        [ObservableProperty]
        private string _searchStrC = string.Empty;

        [RelayCommand]
        private async Task AddNewAsync()
        {
            await ExtentHelper.GetService<IDialogService>()
          .ShowDialog<BlacklistAddView>();
            await LoadFromApi();
        }

        [RelayCommand]
        private async Task SearchAsync()
        {
            IsLoading = true;
            await Task.Delay(200);
            try
            {
                Blacklists.Clear();
                IEnumerable<BlacklistShowModel> res = allData;
                if (!SearchStr.IsNullOrEmpty())
                    res = res.Where(q => q.NameCn.Contains(SearchStr));
                if (!SearchStrC.IsNullOrEmpty())
                    res = res.Where(q => q.NameEn.Contains(SearchStrC));

                foreach (var item in res)
                {
                    Blacklists.Add(item);
                }
            }
            catch (Exception ex)
            {
                GlobalMessage.Instance.ShowError($"数据加载错误：{ex.Message}");
            }
            finally
            { IsLoading = false; }
        }

        [RelayCommand]
        private async Task DeleteAsync(BlacklistShowModel personnel)
        {
            IsLoading = true;
            await Task.Delay(200);
            try
            {
                var res = await _service.BatchDelete([personnel.Id]);
                if (res.StatusCode != 200)
                {
                    GlobalMessage.Instance.ShowError(JSON.Serialize(res.Errors));
                    return;
                }
                GlobalMessage.Instance.ShowSuccess($"数据删除成功！");
                await LoadFromApi();
            }
            catch (Exception ex)
            {
                GlobalMessage.Instance.ShowError($"数据加载错误：{ex.Message}");
            }
            finally
            { IsLoading = false; }
        }

        private async Task LoadFromApi()
        {
            Blacklists.Clear();
            var res = await _service.GetListAsync();
            if (res.StatusCode != 200)
            {
                GlobalMessage.Instance.ShowError(JSON.Serialize(res.Errors));
                return;
            }
            foreach (var item in res.Data)
            {
                Blacklists.Add(item);
            }
        }
    }
}